package com.hongmeng.teamwork.factory.task;

import com.hongmeng.teamwork.entity.dto.TaskMessageDto;
import com.hongmeng.teamwork.entity.dto.TeamworkTaskDto;
import com.hongmeng.teamwork.entity.event.TaskMessageEvent;
import com.hongmeng.teamwork.entity.po.TeamworkTask;
import com.hongmeng.teamwork.enums.TaskOptTypeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Log4j2
@Component
public class TaskOptFactory implements InitializingBean {

    @Resource
    private List<TaskOptHandler> taskOptHandlerList;
    private final Map<TaskOptTypeEnum, TaskOptHandler> handlerMap = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        for (TaskOptHandler taskOptHandler : taskOptHandlerList) {
            handlerMap.put(taskOptHandler.getOptType(), taskOptHandler);
        }
    }

    public String buildMessage(TaskMessageDto taskMessageDto, TaskOptTypeEnum optType) {
        TaskOptHandler taskOptHandler = handlerMap.get(optType);
        if (Objects.isNull(taskOptHandler)) {
            log.error("TaskOptFactory.buildMessage.error: 不支持当前操作任务类型：{}", optType);
            throw new IllegalArgumentException("不支持当前任务操作类型！");
        }
        return taskOptHandler.buildMessage(taskMessageDto);
    }
}
